package com.itheima.entity;

import com.hzheima.util.ImageUtil;
import com.itheima.config.AppConfig;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Plane extends FlyingObject{

    private boolean left;
    private boolean right;

    public boolean isLeft() {
        return left;
    }

    public void setLeft(boolean left) {
        this.left = left;
    }

    public boolean isRight() {
        return right;
    }

    public void setRight(boolean right) {
        this.right = right;
    }

    public Plane() {
        //设置飞机的图片
       img= ImageUtil.getImg("hero.png");

       //初始化飞机尺寸
        width= AppConfig.PLANE_WIDTH/4;
        hight= AppConfig.PLANE_HEIGHT/4;
        speed= 10;

        //设置飞机初始坐标
        x=AppConfig.App_WIDTH/2-width/2;
        y=AppConfig.App_HIGHT-hight-50;

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    Plane.this.moveToLeft();
                    Plane.this.moveToRight();
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

    // 向右移动
    public void moveToRight() {
        if (right) {
            if(x<AppConfig.App_WIDTH-width-20){
                x+=speed;
            }
        }

    }
    //向左移动
    public void moveToLeft(){
        if (left) {
            if(x>0){
                x-=speed;
            }
        }
    }

    //储存子弹
    private ArrayList<Bullet>bullets=new ArrayList<>();

    public ArrayList<Bullet> getArrayList(){
        return bullets;
    }
    public ArrayList<Bullet> getBullets(){
        return bullets;
    }

    //发射子弹
    int count;
    public void shoot(){
        count++;
       if(count%20==0){
           Bullet bullet=new Bullet(this.x,this.y);
           bullets.add(bullet);
           count=0;
       }

        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet=bullets.get(i);

            //子弹向上移动
            bullet.moveUp();

            //删除出屏子弹
            if(bullet.getY()<=-bullet.getHight()){
                bullets.remove(i);
            }
        }
       System.out.println(bullets.size());
    }

    public void changeImg(){
        img=ImageUtil.getImg("boom.png");
    }

    public boolean beCrash(Ball ball){
         boolean b=collide(ball);
         if(b){
            changeImg();
         }
        return b;
    }
}
